package com.xwks.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

/**
 *
 * @author xwks
 * @param <T>
 */
@Data
public class Result<T>{

    private Integer code;

    private String msg;

    private Long total;

    private T data;



    public static <T> Result<T> success() {
        Result result = new Result();
        result.setCode(200);
        result.setMsg("success");
        return result;
    }

    public static <T> Result<T> success(String msg, T data) {
        Result result = new Result();
        result.setCode(200);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(String msg) {
        Result result = new Result();
        result.setCode(200);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result result = new Result();
        result.setCode(200);
        result.setMsg("success");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(IPage<T> page) {
        Result result = new Result();
        result.setCode(200);
        result.setMsg("success");
        result.setTotal(page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    public static <T> Result<T> error() {
        Result result = new Result();
        result.setCode(500);
        result.setMsg("error");
        return result;
    }

    public static <T> Result<T> error(String msg, T data) {
        Result result = new Result();
        result.setCode(500);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.setCode(500);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> error(T data) {
        Result result = new Result();
        result.setCode(500);
        result.setMsg("error");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(Throwable e) {
        Result result = new Result();
        result.setCode(500);
        result.setMsg(e.getMessage());
        return result;
    }

    public static <T> Result<T> result(Integer code, String msg, T data) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}
